package com.fjl.paixu;

import java.io.File;
import java.sql.Date;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;

public class FilePaixun
{
	public static void main(String[] args)
	{
//		File rootFile = new File("F:/fjltest");
//		List<File> fileLst = new ArrayList<File>();
//		// 判断地图路径是否存在
//		if (rootFile.exists())
//		{
//			fileLst = getAllFile(rootFile);
//			printLst(fileLst);
//		}
//		orderByDate("F:/fjltest/Tracks");
		orderByDate(getAllFile(new File("F:/fjltest/Tracks")));
	}

	/**
	 * 获取当前文件夹下所有文件
	 * 
	 * @param rootFile
	 * @return
	 */
	public static List<File> getAllFile(File rootFile)
	{
		// 当前路径下所有文件
		List<File> allFiles = new ArrayList<File>();

		File[] mRootFiles = rootFile.listFiles();
		// 根目录
		for (int i = 0; i < mRootFiles.length; i++)
		{
			if (mRootFiles[i].isFile())
			{
				allFiles.add(mRootFiles[i]);
			}
			else
			{
				// 二级目录
				File[] secondFiles = mRootFiles[i].listFiles();
				for (File secondFile : secondFiles)
				{
					if (secondFile.isFile())
					{
						allFiles.add(secondFile);
					}
					else
					{
						// 三级目录
					}
				}
			}

		}
		printLst(allFiles);
		return allFiles;
	}
	
	// 按日期排序
	public static List<File> orderByDate(List<File> fileLst)
	{
		List<File> orderFileLst = new ArrayList<>();
		File[] fs = new File[fileLst.size()];
		fileLst.toArray(fs);
		Arrays.sort(fs, new Comparator<File>()
		{
			public int compare(File f1, File f2)
			{
//				long diff = f1.lastModified() - f2.lastModified();
				long diff = f2.lastModified() - f1.lastModified();
				if (diff > 0)
					return 1;
				else if (diff == 0)
					return 0;
				else
					return -1;
			}

			public boolean equals(Object obj)
			{
				return true;
			}

		});
		for (int i = fs.length - 1; i > -1; i--)
		{
			System.out.println(fs[i].getName());
			System.out.println(new Date(fs[i].lastModified()));
			orderFileLst.add(fs[i]);
		}
		printLst(orderFileLst);
		return orderFileLst;
	}

	// 按照文件大小排序
	public static void orderByLength(String fliePath)
	{
		List<File> files = Arrays.asList(new File(fliePath).listFiles());
		Collections.sort(files, new Comparator<File>()
		{
			public int compare(File f1, File f2)
			{
				long diff = f1.length() - f2.length();
				if (diff > 0)
					return 1;
				else if (diff == 0)
					return 0;
				else
					return -1;
			}

			public boolean equals(Object obj)
			{
				return true;
			}
		});
		for (File f : files)
		{
			if (f.isDirectory())
				continue;
			System.out.println(f.getName() + ":" + f.length());
		}
	}

	// 按照文件名称排序
	public static void orderByName(String fliePath)
	{
		List<File> files = Arrays.asList(new File(fliePath).listFiles());
		Collections.sort(files, new Comparator<File>()
		{
			@Override
			public int compare(File o1, File o2)
			{
				if (o1.isDirectory() && o2.isFile())
					return -1;
				if (o1.isFile() && o2.isDirectory())
					return 1;
				return o1.getName().compareTo(o2.getName());
			}
		});
		for (File f : files)
		{
			System.out.println(f.getName());
		}
	}

	// 按日期排序
	public static void orderByDate(String fliePath)
	{
		File file = new File(fliePath);
		File[] fs = file.listFiles();
		Arrays.sort(fs, new Comparator<File>()
		{
			public int compare(File f1, File f2)
			{
				long diff = f1.lastModified() - f2.lastModified();
				if (diff > 0)
					return 1;
				else if (diff == 0)
					return 0;
				else
					return -1;
			}

			public boolean equals(Object obj)
			{
				return true;
			}

		});
		for (int i = fs.length - 1; i > -1; i--)
		{
			System.out.println(fs[i].getName());
			System.out.println(new Date(fs[i].lastModified()));
		}
	}

	/**
	 * 打印文件
	 * 
	 * @param fileLst
	 */
	public static void printLst(List<File> fileLst)
	{
		System.out.println("**************");
		for (File file : fileLst)
		{
			System.out.println(file.getName());
			System.out.println(new Date(file.lastModified()));
		}
		System.out.println("================");
	}
}
